<?php

/**
 * Created by PhpStorm.
 * User: wukent
 * Date: 2017/9/25
 * Time: 下午5:59
 */
class PaymentAction extends ApiAction
{

    protected $mainMod;
    protected $usersMod;
    protected function _initialize() {
        parent::_initialize();
        $this->mainMod = D("OfflineOrder");
        $this->usersMod = D("Users");
    }

    public function index(){
        /*
         * $total_money 订单总价
         * $actual_money 实际消费金额
         * $actual_points 实际消费积分
         * $merchant_id 商家ID
         * */

        //订单号生成
        $out_trade_no = "m".time().rand(1000,9999).rand(100,999);

        $total_money = $this->_post('total_money');
        $actual_money = $this->_post('actual_money');
        $actual_points = $this->_post('actual_points');
        $merchant_id = $this->_post('merchant_id');
        $ordertime = time();
        //$pay_type 付款类型 1为支付宝，2为微信，3为现金(即只消费积分)
        $pay_type = $this->_post('pay_type');
        $order_id = $this->mainMod->entry($this->uid,$out_trade_no,$ordertime);


        if ($order_id<0){
            $this->rest('',20);
        }


        if ($pay_type == 1){


            vendor('alipaysdk.aop.AopClient');
            vendor('alipaysdk.aop.request.AlipayTradeAppPayRequest');


            $aop = new AopClient;
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = "2017080408031021";
            $aop->rsaPrivateKey = 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCS7cOhZCZTKBp+0+os+FEr1WUUDSzH8HfpXvu2wMlWy8v1Mpuuu4HraksS/LksClw7B6ZHXsciMyGdnGPJIERrBCOCPtvtt9BxTDHWUIbBwAxs8o305Q01xlg1YAljKpcr4eyhhUo7t6tkwcMOx+KQChKve/ALRJAOFxA+sjEg20pzKgx1HDk145sXxdx0/9SQP5yTd0fHGWdFQFMWzKW629EXVhJuwmWkSYVjT54RIquSAqI1CTNpGm4cHAiOK8YHsE9VsQPBNcpx4nltw3O/QA8gzhn4sH96yJpIeMYLq0cR4PKIhgLwNg0v8Olqbq8P3TLMyuJZqvh0Q67DQvxFAgMBAAECggEBAI1t6N21dV6eP8LNYaCf9iJnasB+XhinVAW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R3t36HhNBH3SY3ktDhX4wP2Nze5I2fUeCAil/qc+7AOcZGjEYrEYOTljOrVIVzEfSaycvG7QXjQIal1wOhPqLXUNFBd+LowWf6nzrdmf0YBYw==' ;
            //$aop->format = "json";
            $aop->charset = "utf-8";
            $aop->signType = "RSA2";
            //$aop->alipayrsaPublicKey = '请填写支付宝公钥，一行字符串';
            $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApV/jU0SgPXt8uryI2TxpAwBP+gbf6IZRrpjWoa4LYoVQxjXqXumm17d0vSHrYblQAlehQiQnm6lW2PXeIa7+pAtghUn9qdAg6eM/46r8nQ5iOVnQUtcHwxUChdm5C6OjzeHpFN0qxpoYZKpUs2COOwE5IiQkaTiJkP0I6TC1D5qoap2StCpcKJFPnXvjybLQjesbznTx5KPrAh+G4cUTCHDPiHZSd+iV7QPRnkN5gtQjpO187+hD80VzhwHVKNHpWALzOQ728W8wJcFvaIGyy1Oku0WPDNsHPEcCtp4D+L2wN/Zy2BjwW0JjdXYOi8QpXMW818JDKHBsFsZKMNlj0QIDAQAB';
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            $request = new AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数
            $bizcontent = "{\"subject\": \"食安购\","
                . "\"out_trade_no\": \"{$out_trade_no}\","
                . "\"timeout_express\": \"30m\","
                . "\"total_amount\": \"{$actual_money}\","
                . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                . "}";

            $request->setNotifyUrl("http://test.shiangou.com.cn/merchant/api.php/PayNotify/notifycheck");
            $request->setBizContent($bizcontent);
            //这里和普通的接口调用不同，使用的是sdkExecute
            $response = $aop->sdkExecute($request);

            $res['pay_type'] = $pay_type;
            $res['ali_secret'] = $response;
            $this->rest($res,1);


        }elseif ($pay_type == 2){
            vendor('Weixin.Zhifu');
            $config = array(
                'appid' =>'wx91470ddde903ce11',/*微信开放平台上的应用id*/
                'mch_id' =>'1487655792',/*微信申请成功之后邮件中的商户id*/
                'api_key' =>'be4bda5e208208cc522d0c63a7543f94',//在微信商户平台上自己设定的api密钥 32位  
                'notify_url' => "http://test.shiangou.com.cn/merchant/ThinkPHP/Extend/Vendor/Weixin/lib/WxPay.Notify.php"
            );
            $obj  = new ZhifuAPI($config);
            $sign = $obj->orderquery($order_id,$actual_money,$order_id);
            $sign['data']['pay_type'] = $pay_type;
            $res = $sign['data'];
            $this->rest($res,1);
        }elseif ($pay_type == 3){
            $userinfo = $this->usersMod->getInfo($this->uid);
            $points = $userinfo['rank_points']-$actual_points;
            if ($this->usersMod->uppoints($this->uid,$points)){
                $res['pay_type'] = $pay_type;
                $this->rest($res,1);

            }else{
                $this->rest('',21);
            }

        }
    }





}